package gpl.scotlandyard.ui.dialogs;

import gpl.scotlandyard.beans.adapters.PlayerMovesTM;
import gpl.scotlandyard.utils.Config;
import gpl.scotlandyard.utils.I18n;
import gpl.scotlandyard.utils.ImageProvider;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.apache.log4j.Logger;

/**
 * Trace player moves with TracerTM.
 * @author Norbert Martin
 */
public class TracerDialog {

  private static final Logger _log = Logger.getLogger(SpecialDialog.class);
  private final JDialog jdialog = new JDialog();
  private final PlayerMovesTM model;
  private final JTable table = new JTable();
  private boolean isInit;

  /**
   * Creates a tracer.
   * @param ftm
   */
  public TracerDialog(PlayerMovesTM ftm) {
    ftm.getClass();
    model = ftm;
  }

  /**
   * Sets visibles.
   * @param b
   */
  public void setVisible(boolean b) {
    if (b && !isInit) {
      init();
    }
    if (b) {
      _log.info("player tracer showed");
    } else {
      _log.info("player tracer hidden");
    }
    jdialog.setVisible(b);
  }

  /**
   * Initializes content.
   */
  private void init() {
    jdialog.setIconImage(ImageProvider.get(Config.get("EYE_IMG")).getImage());
    jdialog.setLocationByPlatform(true);
    jdialog.setAlwaysOnTop(true);
    jdialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    jdialog.setTitle(String.format(I18n.get("TRACER_TITLE"), model.getName()));
    table.setModel(model);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    jdialog.getContentPane().add(new JScrollPane(table));
    jdialog.pack();
    jdialog.setSize(jdialog.getWidth(), 300);
  }

}
